$(function() {

    //勾选
    $('.view-cent-left .checkbox-label').click(function() {
        if ($(this).find('input').attr("checked")) {
            $(this).find('input').removeAttr('checked');
        } else {
            $(this).find('input').attr('checked', '');
        }
        $(this).toggleClass('checked');
    })

    //删除
    $('.view-cent-left .hide').click(function() {
        $(this).parent('.view-cent-left li').animate({
            left: '99px',
            opacity: '0'
        }, function() {
            $(this).remove();
        })
    })

    /*加*/
    var box = $('.cent-left-view'),
        prices = box.find('.price span'),
        ctrlBox = box.find('input');

    $('body').on('click', '.cent-left-view .plus', function() {
        var thisIndex = $(this).parents('li').index(),
            thisNumBox = $(this).siblings('input'),
            thisPrice = $(this).parents('li').attr('data-price'),
            thisPriceBox = prices.eq(thisIndex),
            thisCtrlBox = ctrlBox.eq(thisIndex);

        if (thisCtrlBox.val() < 9) {
            thisCtrlBox.val(parseInt(thisCtrlBox.val()) + 1)
        } else {
            thisCtrlBox.val(parseInt(thisCtrlBox.val()))
        }

        thisPrice * thisNumBox.val().replace()
        var newVal = (thisPrice * thisNumBox.val()).toString(),
            writeVal = '';
        for (var i = 0; i < newVal.length; i++) {
            if (newVal.length - i == 3) {
                writeVal += ',' + newVal[i];
            } else {
                writeVal += newVal[i];
            }
        }
        thisPriceBox.text(writeVal);
    });

    //减

    $('body').on('click', '.cent-left-view .minus', function() {

        var thisIndex = $(this).parents('li').index(),
            thisNumBox = $(this).siblings('input'),
            thisPrice = $(this).parents('li').attr('data-price'),
            thisPriceBox = prices.eq(thisIndex),
            thisCtrlBox = ctrlBox.eq(thisIndex);

        if (thisCtrlBox.val() > 0) {
            thisCtrlBox.val(parseInt(thisCtrlBox.val()) - 1)
        } else {
            thisCtrlBox.val(parseInt(thisCtrlBox.val()))
        }

        thisPrice * thisNumBox.val().replace()
        var newVal = (thisPrice * thisNumBox.val()).toString(),
            writeVal = '';
        for (var i = 0; i < newVal.length; i++) {
            if (newVal.length - i == 3) {
                writeVal += ',' + newVal[i];
            } else {
                writeVal += newVal[i];
            }
        }
        thisPriceBox.text(writeVal);
    });

    $('.view-cent-left li').hover(function() {
        $(this).find('.hide').stop().fadeIn();
    }, function() {
        $(this).find('.hide').stop().fadeOut();
    })

    //第一次

    //所在地区边框颜色

    $('.view-region i').hover(function() {
        $(this).css({
            'border-color': '#e8b768'
        });
        $(this).next('ul').css({
            'border-color': '#e8b768'
        });
    }, function() {
        $(this).css({
            'border-color': '#323232'
        });
        $(this).next('ul').css({
            'border-color': '#323232'
        });
    })

    $('.view-region ul').hover(function() {
        $(this).css({
            'border-color': '#e8b768'
        });
        $(this).prev('i').css({
            'border-color': '#e8b768'
        });
    }, function() {
        $(this).css({
            'border-color': '#323232'
        });
        $(this).prev('i').css({
            'border-color': '#323232'
        });
    })

    /*单选（设置默认收货地址）*/

    $('body').on("click", ".view-default", function() {
        if ($(this).find('input').attr("checked")) {
            $(this).find('input').removeAttr('checked');
        } else {
            $(this).find('input').attr('checked', '');
        }
        $(this).toggleClass('checked');
    })

    //input焦点

    var name = $('.view-name input'); //赋值姓名
    var iphone = $('.view-iphone input'); //赋值手机号码
    var mailbox = $('.view-mailbox input'); //赋值邮箱
    var address = $('.view-address textarea'); //赋值详细地址

    //姓名焦点

    name.focus(function() {
        $(this).css({
            'border-color': '#e8b768',
            'transition': '1s'
        });
    })
    name.blur(function() {
        $(this).css({
            'border-color': '#313131',
            'transition': '1s'
        });
    })

    //手机号码焦点

    iphone.focus(function() {
        $(this).css({
            'border-color': '#e8b768',
            'transition': '1s'
        });
    })
    iphone.blur(function() {
        $(this).css({
            'border-color': '#313131',
            'transition': '1s'
        });
    })

    //邮箱焦点

    mailbox.focus(function() {
        $(this).css({
            'border-color': '#e8b768',
            'transition': '1s'
        });
    })
    mailbox.blur(function() {
        $(this).css({
            'border-color': '#313131',
            'transition': '1s'
        });
    })

    //详细地址焦点

    address.focus(function() {
        $(this).css({
            'border-color': '#e8b768',
            'transition': '1s'
        });
    })
    address.blur(function() {
        $(this).css({
            'border-color': '#313131',
            'transition': '1s'
        });
    })

    $('.one-view-left .button').click(function() {

        //判断姓名

        if (name.val() == '') {
            $('.view-name strong').html('请输入姓名！')
        } else {
            $('.view-name strong').html('')
        }
        if (iphone.val() == '') {

            //判断手机号码

            $('.view-iphone strong').html('请输入手机号码！')
        } else if (checkMobile(iphone.val())) {
            $('.view-iphone strong').html('')
        } else {
            $('.view-iphone strong').html('您输入的手机号码不合法！')
        }

        //判断邮箱

        if (mailbox.val() == '') {
            $('.view-mailbox strong').html('请输入电子邮箱！')
        } else if (checkmail(mailbox.val())) {
            $('.view-mailbox strong').html('')
        } else {
            $('.view-mailbox strong').html('您输入的邮箱不合法！')
        }

        //判断所在地区

        if ($('.region-area i').html() == '请选择地区') {
            $('.view-region strong').html('请输入地区')
        } else {
            $('.view-region strong').html('')
        }

        //判断详细地址

        if (address.val() == '') {
            $('.view-address strong').html('请输入详细地址！')
        } else {
            $('.view-address strong').html('')
        }
    })

    //支付方式

    $('.payment-view .prompt').hover(function() {
        $(this).find('span').show(200);
    }, function() {
        $(this).find('span').hide(200);
    })

    $('.prompt span').hover(function() {
        $('.prompt-view').show(200);
    }, function() {
        $('.prompt-view').hide(200);
    })

    //弹窗input验证
    //弹窗input焦点
    var name = $('#name'),
        phone = $('#phone'),
        mail = $('#mail'),
        detailed = $('#detailed');

    name.focus(function() {
        $(this).css({
            'border-color': '#e8b768',
            'transition': '1s'
        });
    })
    name.blur(function() {
        $(this).css({
            'border-color': '#323232',
            'transition': '1s'
        });
    })
    phone.focus(function() {
        $(this).css({
            'border-color': '#e8b768',
            'transition': '1s'
        });
    })
    phone.blur(function() {
        $(this).css({
            'border-color': '#323232',
            'transition': '1s'
        });
    })
    mail.focus(function() {
        $(this).css({
            'border-color': '#e8b768',
            'transition': '1s'
        });
    })
    mail.blur(function() {
        $(this).css({
            'border-color': '#323232',
            'transition': '1s'
        });
    })
    detailed.focus(function() {
        $(this).css({
            'border-color': '#e8b768',
            'transition': '1s'
        });
    })
    detailed.blur(function() {
        $(this).css({
            'border-color': '#323232',
            'transition': '1s'
        });
    })

    $('.preservation .address').click(function() {
        $('.address-click').show();
    })

    $('.address-input .button').click(function() {
        if (name.val() == '') {
            $('.name .prompt').html('名字为空')
            console.log('名字为空')
        } else {
            $('.name .prompt').html('')
        }
        if (!checkMobile(phone.val())) {
            $('.phone .prompt').html('请输入11位正确号码')
            console.log('号码为空')
        } else {
            $('.phone .prompt').html('')
        }
        if (!checkmail(mail.val())) {
            $('.mail .prompt').html('请输入正确邮箱格式')
            console.log('邮箱为空')
        } else {
            $('.mail .prompt').html('')
        }
        if ($('.region-area i').html() == '区') {
            $('.view-region .prompt').html('请选择地区')
            console.log('地区为空')
        } else {
            $('.view-region .prompt').html('')
        }
        if (detailed.val() == '') {
            $('.detailed .prompt').html('详细地址为空')
            console.log('详细地址为空')
        } else {
            $('.detailed .prompt').html('')
        }
        if (name.val() == '' || !checkMobile(phone.val()) || !checkmail(mail.val()) || $('.region-area i').html() == '区' || detailed.val() == '') {} else {
            $('.address-input .prompt').html('')
                //收货地址
            var region_province = $('.region-province i').html(),
                region_city = $('.region-city i').html(),
                region_area = $('.region-area i').html();
            var receipt_address = '<li class=fl><div class=preservation-tit>' + name.val() + '<span class="f12 cc6">（' + region_province + '   ' + region_city + '）</span></div><div class="preservation-cent f12 cc6">' + region_area + '    ' + detailed.val() + '    ' + phone.val() + '</div><div class="preservation-foot disnone f12"><span class="preservation-default">设为默认</span><span class="preservation-modify">修改</span></div><div class="preservation-hide"></div></li>';
            $('.preservation .address').before(receipt_address);
            $('.address-click').hide();
            //弹窗表单初始化
            name.val('');
            phone.val('');
            $('.region-area i').html('区');
            mail.val('')
            $('.region-province i').html('省');
            $('.region-city i').html('市');
            detailed.val('');
            //删除
            $('.preservation .preservation-hide').click(function() {
                $(this).parent('.preservation li').animate({
                    left: '99px',
                    opacity: '0'
                }, function() {
                    $(this).remove();
                })
            })
            $('.preservation li').hover(function() {
                $(this).find('.preservation-foot').fadeIn()
            }, function() {
                $(this).find('.preservatiossn-foot').fadeOut()
            })
        }
        $('.preservation-modify').click(function() {
            $('.address-click').fadeIn();
            console.log('2222222')
        })
    })

    //关闭弹窗
    $('.address .address-hide').click(function() {
        $('.address-click').hide();
    })

    //校验手机号
    function checkMobile(str) {
        var rephone = /^((\d{8})|(1\d{10}))$/
            // var rephone = /^(\d+)$/
        if (rephone.test(str)) {
            return true;
        } else {
            return false;
        }
    }

    // 邮箱正则
    function checkmail(strmail) {
        var remail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
            // var rephone = /^(\d+)$/
        if (remail.test(strmail)) {
            return true;
        } else {
            return false;
        }
    }

})
